<template>
  <el-dialog width="470px" class="app-dialog" title="下载APP" :visible.sync="visible" :before-close="handleClose">
    <div class="content">
      <div>
        <img :src="ios" width="150" style="text-align: center" />
        <div>苹果下载二维码</div>
      </div>
      <div v-if="isAndirod">
        <img :src="andirod" width="150" style="text-align: center" />
        <div>安卓下载二维码</div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import QRCode from 'qrcode'
import { getAppVersions } from '@/api/appVersion'
export default {
  name: 'AppDownload',
  data: function () {
    return {
      andirod: '',
      ios: '',
      isAndirod: false,
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    downloadText: function () {
      return (this.appType == '安卓' ? 'Android' : this.appType == '苹果' ? 'IOS' : '') + '下载二维码'
    },
  },
  methods: {
    async init() {
      return getAppVersions().then(res => {
        const data = res && res.items
        this.$nextTick(() => {
          this.isAndirod = data[0].fullPackageUpdate
          QRCode.toDataURL(data[0].packageAddress, { width: 150, margin: 1 }).then(res => {
            this.andirod = res
          })
          QRCode.toDataURL('https://apps.apple.com/cn/app/%E9%95%94%E9%91%AB%20Link/id6503027870', { width: 150, margin: 1 }).then(res => {
            this.ios = res
          })
        })
      })
    },
    handleClose() {
      this.$emit('close', false)
    },
  },
}
</script>

<style lang="scss" scoped>
.content {
  padding: 0.4rem 0;
  text-align: center;
  div {
    font-size: 14px;
    color: #333333;
    margin: 10px 0;
    font-style: normal;
  }
}
</style>
